package jiali.config;

import jiali.model.vo.StandardVO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.subject.Subject;

import java.util.Collections;

public class UserRealmConfig extends AuthorizingRealm {

    //授权
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        System.out.println("执行授权。。。");
        SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
        Subject subject = SecurityUtils.getSubject();
        //假设连接了数据库
        StandardVO standardVO = (StandardVO) subject.getPrincipal();
        int status = standardVO.getStatus();
        info.addStringPermission(String.valueOf(status));
        return info;
    }

    //认证
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        System.out.println("执行认证。。。");

        //用户名密码
        String name = "root";
        String password = "123456";
        UsernamePasswordToken userToken = (UsernamePasswordToken) token;

        //如果有真实的对象，而且想要使用授权功能，下面的principal也要改成相对应的对象才可以
        return name.equals(userToken.getUsername()) ? new SimpleAuthenticationInfo("", password, "") : null;

    }
}
